﻿#include "MaterialDialogFoam.h"
#include "ui_MaterialDialogFoam.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsMaterialsFoam.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include <QMessageBox>

namespace GUI 
{

    MaterialDialogFoam::MaterialDialogFoam(PhysicsDlgExecType type, int matid /*= -1*/, QWidget* parent /*= nullptr*/)
        :AbstractMaterialDialog(type, parent)
    {
        _ui = new Ui::MaterialDialogFoam;
        _ui->setupUi(this);
        //获取材料对象
        if (_matMgr && matid > 0)
            _mat = dynamic_cast<Acoustics::FITKAcousticsMaterialsFoam*>(_matMgr->getDataByID(matid));

        this->init();

    }
    MaterialDialogFoam::~MaterialDialogFoam()
    {
        if (_ui) delete _ui;
    }
    void MaterialDialogFoam::on_pushButtonOK_clicked()
    {
        if (!this->checkInput()) return;
        //要处理的对象
        Acoustics::FITKAcousticsMaterialsFoam* foam = dynamic_cast<Acoustics::FITKAcousticsMaterialsFoam*>(_mat);
        //保存执行的脚本
        QStringList str;
        QString name = _ui->lineEdit_Name->text();
        //创建与复制,创建新对象
        if (_execType == GUI::PhysicsDlgExecType::Create || _execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            str.append("m=Material.Foams()");
            str.append(QString("m.createMaterial('%1')").arg(name));

            foam = new Acoustics::FITKAcousticsMaterialsFoam;
            _matMgr->appendDataObj(foam);
        }
        if (!foam)return;
        //保存数据
        double density= _ui->lineEdit_Density->text().toDouble();
        double flowResistivity= _ui->lineEdit_FlowResistivity->text().toDouble();
        double porosity= _ui->lineEdit_Porosity->text().toDouble();
        double shear= _ui->lineEdit_shearModulus->text().toDouble();
        double tortuosity= _ui->lineEdit_Tortuosity->text().toDouble();
        double viscousLength= _ui->lineEdit_Viscous->text().toDouble();
        double thermalLength= _ui->lineEdit_Thermal->text().toDouble();
        double dlf= _ui->lineEdit_DampingLossFactor->text().toDouble();
        double young= _ui->lineEdit_YoungModulus->text().toDouble();
        double poissonRatio= _ui->lineEdit_PoissonRatio->text().toDouble();

        if (_execType == GUI::PhysicsDlgExecType::Edit)
        {
            str.append(QString("m=Material.Foams.GetFoams('%1')").arg(foam->getDataObjectName()));
            str.append(QString("m.setName('%1')").arg(name));
        }
        str.append(QString("m.setDensity(%1)").arg(density));
        str.append(QString("m.setModulus(%1,%2,%3)").arg(shear).arg(young).arg(poissonRatio));
        str.append(QString("m.setOtherProp(%1,%2,%3,%4,%5,%6)").arg(flowResistivity).arg(porosity).
            arg(tortuosity).arg(viscousLength).arg(thermalLength).arg(dlf));

        foam->setDataObjectName(name);
        foam->setDensity(density);
        foam->setTensileModulus(young);
        foam->setPoissonRatio(poissonRatio);
        foam->setLossFactor(dlf);
        foam->setPorosity(porosity);
        foam->setshearModulus(shear);
        foam->setTortuosity(tortuosity);
        foam->setThermalLength(thermalLength);
        foam->setViscousLength(viscousLength);
        foam->setFlowResistivity(flowResistivity);

        this->saveScript(str);
        this->accept();
    }
    void MaterialDialogFoam::on_pushButtonCancel_clicked()
    {
        this->reject();
    }
    void MaterialDialogFoam::init()
    {
        if (_matMgr == nullptr) return;

        if (_execType == GUI::PhysicsDlgExecType::Create)
        {
            QString name = _matMgr->checkName("New Foam Material");
            _ui->lineEdit_Name->setText(name);
        }
        Acoustics::FITKAcousticsMaterialsFoam* foam = dynamic_cast<Acoustics::FITKAcousticsMaterialsFoam*>(_mat);
        if (!foam)return;

        // 设置各个属性的值
        _ui->lineEdit_Name->setText(foam->getDataObjectName());
        _ui->lineEdit_Density->setText(QString::number(foam->getDensity()));
        _ui->lineEdit_YoungModulus->setText(QString::number(foam->getTensileModulus()));
        _ui->lineEdit_PoissonRatio->setText(QString::number(foam->getPoissonRatio()));
        _ui->lineEdit_DampingLossFactor->setText(QString::number(foam->getLossFactor()));
        _ui->lineEdit_Porosity->setText(QString::number(foam->getPorosity()));
        _ui->lineEdit_shearModulus->setText(QString::number(foam->getshearModulus()));
        _ui->lineEdit_Tortuosity->setText(QString::number(foam->getTortuosity()));
        _ui->lineEdit_Thermal->setText(QString::number(foam->getThermalLength()));
        _ui->lineEdit_Viscous->setText(QString::number(foam->getViscousLength()));
        _ui->lineEdit_FlowResistivity->setText(QString::number(foam->getFlowResistivity()));

        //复制,更新名称
        if (_execType == GUI::PhysicsDlgExecType::Duplicate)
        {
            QString text = QString("Copy of %1").arg(foam->getDataObjectName());
            _ui->lineEdit_Name->setText(text);
        }
    }

    bool MaterialDialogFoam::checkInput()
    {
        //检查数据合法性
        QList<QLineEdit*> checkUi;
        checkUi << _ui->lineEdit_Density << _ui->lineEdit_YoungModulus << _ui->lineEdit_PoissonRatio
            << _ui->lineEdit_DampingLossFactor << _ui->lineEdit_Porosity << _ui->lineEdit_shearModulus
            << _ui->lineEdit_Tortuosity << _ui->lineEdit_Thermal << _ui->lineEdit_Viscous
            << _ui->lineEdit_FlowResistivity;
        for (QLineEdit* e : checkUi)
        {
            if (e == nullptr) continue;
            QString text = e->text().trimmed();
            bool ok = false;
            text.toDouble(&ok);
            if (!ok)
            {
                QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a number."));
                return false;
            }
        }
        if (_matMgr == nullptr) return false;
        //检查名称
        QString text = _ui->lineEdit_Name->text().trimmed();
        if (text != "") {
            QString newText = _matMgr->checkName(text);
            if (text == newText) return true;
            //与自己重名
            if (_matMgr->getDataByName(text) == _mat) return true;
        }

        QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a new Name."));
        return false;
    }
}

